package com.chenyue.cm.utils;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Administrator on 2019-12-26.
 */
public class MapUtil {


    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, Object> sortMapByKey(Map<String, Object> map , boolean asc) {
        if (map == null || map.isEmpty()) {
            return null;
        }
//        Map<String, String> sortMap = new TreeMap<String, String>(new MapKeyComparator());
        Map<String, Object> sortMap = new TreeMap<String, Object>(new Comparator<String>() {
            public int compare(String obj1, String obj2) {
                return asc ? obj1.compareTo(obj2): obj2.compareTo(obj1) ;//升序排序
            }
        });
        sortMap.putAll(map);
        return sortMap;
    }


}
